package com.example.warehouse.sys.controller;

import com.example.warehouse.common.pojo.vo.PageData;
import com.example.warehouse.common.web.JsonResult;
import com.example.warehouse.core.excel.ExcelUtil;
import com.example.warehouse.sys.pojo.entity.SysDept;
import com.example.warehouse.sys.pojo.entity.SysRole;
import com.example.warehouse.sys.pojo.param.AddNewRoleParam;
import com.example.warehouse.sys.pojo.param.SysRoleStandardParam;
import com.example.warehouse.sys.pojo.vo.SysRoleListItemVO;
import com.example.warehouse.sys.service.IRoleService;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/sys/role")
@Api(tags = "3.5. 角色管理")
@Validated
public class RoleController {
    @Autowired
    private IRoleService roleService;

    public RoleController() {
        log.debug("创建控制器类对象：RoleController");
    }

    @ApiOperation("角色列表")
    @ApiOperationSupport(order = 420)
    @GetMapping("/list")
    @PreAuthorize("hasAuthority('/sys/role/read')")
    public JsonResult list() {
        log.debug("开始处理【查询角色列表】的请求");
        return JsonResult.ok(roleService.list());
    }

    @GetMapping("{id:[0-9]+}")
    @PreAuthorize("hasAuthority('/sys/role/read')")
    @ApiOperation("根据id查询")
    @ApiOperationSupport(order = 420)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "id", defaultValue = "1", dataType = "long")
    })
    public JsonResult getRoleById(@PathVariable Long id){
        SysRoleListItemVO role = roleService.getRoleById(id);
        return JsonResult.ok(role);
    }

    @GetMapping("")
    @PreAuthorize("hasAuthority('/sys/role/read')")
    @ApiOperation("查询角色列表")
    @ApiOperationSupport(order = 420)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "页码", defaultValue = "1", dataType = "long")
    })
    public JsonResult list(Integer page) {
        log.debug("开始处理【查询角色列表】的请求，页码：{}", page);
        if (page == null) {
            page = 1;
        }
        Integer pageNum = page > 0 ? page : 1;
        PageData<SysRoleListItemVO> pageData;
            pageData = roleService.list(pageNum);
        return JsonResult.ok(pageData);
    }

    @ApiOperation("新增角色")
    @ApiOperationSupport(order = 100)
    @PostMapping("insert/addRole")
    @PreAuthorize("hasAuthority('/sys/role/insert/addrole')")
    public JsonResult insert(@Validated AddNewRoleParam addNewRoleParam) {
        log.debug("开始处理 [新增角色] 的请求, 参数: {} ", addNewRoleParam);
        roleService.addNew(addNewRoleParam);
        return JsonResult.ok();

    }

    @ApiOperation("删除角色")
    @ApiOperationSupport(order = 200)
    @PostMapping("/{id:[0-9]+}/delete/delRole")
    @PreAuthorize("hasAuthority('/sys/role/delete/delrole')")
    public JsonResult deleteById(@PathVariable @Validated @Range(min = 1, message = "删除角色失败, 请提交合法参数!") Long id) {
        log.debug("开始处理 [删除用户] 的请求, 参数: {} ", id);
        roleService.deleteById(id);
        return JsonResult.ok();

    }

    @ApiOperation("修改角色")
    @ApiOperationSupport(order = 310)
    @PostMapping("/{id:[0-9]+}/update/upRole")
    @PreAuthorize("hasAuthority('/sys/role/update/uprole')")
    public JsonResult updateBySysUser(@Validated SysRoleStandardParam sysRoleStandardParam,
                                      @PathVariable @Range(min = 1, message = "请提交有效的类别ID值！") Long id) {
        log.debug("开始处理 [修改用户参数] 的请求, 参数: {} ", sysRoleStandardParam);
        roleService.updateById(id,sysRoleStandardParam);
        return JsonResult.ok();

    }

    @ApiOperation("权限数据导出")
    @ApiOperationSupport(order = 430)
    @GetMapping("/export")
    public void export(HttpServletResponse response)
            throws IOException {
        String fileName = "权限表";
        List<SysRole> list = roleService.selectAll();
        ExcelUtil.download(response, fileName, SysRole.class, list
        );
    }
}
